package org.lzuploader.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.listeners.recap.CloseSelectionListener;
import org.lzuploader.gui.listeners.recap.ListSelectionListener;
import org.lzuploader.model.Localization;
import org.lzuploader.model.result.Result;
import org.lzuploader.model.result.ResultCharacter;

/**
 * Class used to display a window containing the recap of an upload process. 
 * @author Emmanuel Cron
 *
 */
public class WRecap
{
    /** Width of the window. */
    private static final int WINDOW_WIDTH = 330;
    /** Height of the window. */
    private static final int WINDOW_HEIGHT = 255;
    
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(WRecap.class);
    /** Localization of this class. */
    private static Localization localization = Localization.getInstance();
    
    /* COMPONENTS */
    
    /** List containing the names of the updated characters. */
    private List charactersList;
    
    /** Character update result label. */
    private Label characterUpdateStatus;
    
    /** Professions deleted result label. */
    private Label professionsDeletedStatus;
    
    /** Crafts updated result label. */
    private Label crafatsUpdatedStatus;
    
    /** Reagents updated result label. */
    private Label reagentsUpdatedStatus;
    
    /** Dungeons updated result label. */
    private Label dungeonsUpdatedStatus;
    
    /** Spells updated result label. */
    private Label craftsSpecializationsUpdatedStatus;
    
    /* END OF COMPONENTS */
    
    /** Result object used to display this window's information. */
    private Result result;
    
    /** Shell used to display this window. */
    private Shell shell;
    
    /**
     * Builds a new recap window.
     * @param parent Window that called this process.
     * @param r A {@link Result} object containing the results of an upload process.
     */
    public WRecap(final Shell parent, final Result r)
    {
        log.debug("Creating recap window.");
        
        log.trace("Saving result.");
        
        result = r;
        
        log.trace("Creating shell.");
        
        shell = new Shell(parent, SWT.APPLICATION_MODAL + SWT.DIALOG_TRIM);
        shell.setData(this);
        
        Position.centerElement(parent, shell, WINDOW_WIDTH, WINDOW_HEIGHT);
        shell.setText(localization.getString("DIALOG_TITLE_RECAP"));
        
        log.trace("Opening window.");
        
        shell.open();
        
        populateWindow();
        fillComponents();
    }
    
    /**
     * Populates the recap window with various controls.
     */
    private void populateWindow()
    {
        log.debug("Populating window.");
        
        log.trace("Creating main group.");
        
        final Group mainGroup = new Group(shell, SWT.SHADOW_NONE);
        mainGroup.setLocation(10, 5);
        mainGroup.setSize(305, 67);
        mainGroup.setText(localization.getString("GROUP_UPLOAD_RESULTS"));
        
        log.trace("Creating labels in main group.");
        
        final Label itDel = new Label(mainGroup, SWT.NONE);
        itDel.setLocation(10, 15);
        itDel.setText(localization.getString("LABEL_ITEMS_DELETED") + ":");
        itDel.pack();
        
        final Label itDelNb = new Label(mainGroup, SWT.RIGHT);
        itDelNb.setLocation(100, 15);
        itDelNb.setSize(30, 15);
        itDelNb.setText(String.valueOf(result.getItemsDeleted()));
        
        final Label itUpd = new Label(mainGroup, SWT.NONE);
        itUpd.setLocation(10, 30);
        itUpd.setText(localization.getString("LABEL_ITEMS_UPDATED") + ":");
        itUpd.pack();
        
        final Label itUpdNb = new Label(mainGroup, SWT.RIGHT);
        itUpdNb.setLocation(100, 30);
        itUpdNb.setSize(30, 15);
        itUpdNb.setText(String.valueOf(result.getItemsUpdated()));
        
        final Label stUp = new Label(mainGroup, SWT.NONE);
        stUp.setLocation(10, 45);
        stUp.setText(localization.getString("LABEL_STATS_UPDATED") + ":");
        stUp.pack();
        
        final Label stUpNb = new Label(mainGroup, SWT.RIGHT);
        stUpNb.setLocation(100, 45);
        stUpNb.setSize(30, 15);
        if (result.isStatusUpdated())
        {
            stUpNb.setText(localization.getString("LABEL_OK"));
        }
        else
        {
            stUpNb.setText(localization.getString("LABEL_ERROR"));
        }
        
        final Label spDel = new Label(mainGroup, SWT.RIGHT);
        spDel.setLocation(160, 15);
        spDel.setText(localization.getString("LABEL_SPELLS_DELETED") + ":");
        spDel.pack();
        
        final Label spDelNb = new Label(mainGroup, SWT.RIGHT);
        spDelNb.setLocation(250, 15);
        spDelNb.setSize(30, 15);
        spDelNb.setText(String.valueOf(result.getSpellsDeleted()));
        
        final Label spUpd = new Label(mainGroup, SWT.RIGHT);
        spUpd.setLocation(160, 30);
        spUpd.setText(localization.getString("LABEL_SPELLS_UPDATED") + ":");
        spUpd.pack();
        
        final Label spUpdNb = new Label(mainGroup, SWT.RIGHT);
        spUpdNb.setLocation(250, 30);
        spUpdNb.setSize(30, 15);
        spUpdNb.setText(String.valueOf(result.getSpellsUpdated()));
        
        log.trace("Creating characters list.");
        
        charactersList = new List(shell, SWT.SINGLE + SWT.BORDER);
        charactersList.setLocation(10, 80);
        charactersList.setSize(140, 105);
        charactersList.addSelectionListener(new ListSelectionListener());
        
        log.trace("Creating characters group.");
        
        final Group cGroup = new Group(shell, SWT.SHADOW_NONE);
        cGroup.setLocation(155, 75);
        cGroup.setSize(160, 110);
        cGroup.setText(localization.getString("GROUP_CHARACTER_RESULTS"));
        
        log.trace("Creating characters group labels.");
        
        final Label cUpdate = new Label(cGroup, SWT.NONE);
        cUpdate.setLocation(10, 15);
        cUpdate.setText(localization.getString("LABEL_CHARACTER_UPDATE") + ":");
        cUpdate.pack();
        
        characterUpdateStatus = new Label(cGroup, SWT.RIGHT);
        characterUpdateStatus.setLocation(80, 15);
        characterUpdateStatus.setSize(65, 15);
        
        final Label pDeleted = new Label(cGroup, SWT.NONE);
        pDeleted.setLocation(10, 30);
        pDeleted.setText(localization.getString("LABEL_PROFESSIONS_UPDATE") + ":");
        pDeleted.pack();
        
        professionsDeletedStatus = new Label(cGroup, SWT.RIGHT);
        professionsDeletedStatus.setLocation(80, 30);
        professionsDeletedStatus.setSize(65, 15);
        
        final Label cUpdated = new Label(cGroup, SWT.NONE);
        cUpdated.setLocation(10, 45);
        cUpdated.setText(localization.getString("LABEL_CRAFTS_UPDATE") + ":");
        cUpdated.pack();
        
        crafatsUpdatedStatus = new Label(cGroup, SWT.RIGHT);
        crafatsUpdatedStatus.setLocation(80, 45);
        crafatsUpdatedStatus.setSize(65, 15);
        
        final Label rUpdated = new Label(cGroup, SWT.NONE);
        rUpdated.setLocation(10, 60);
        rUpdated.setText(localization.getString("LABEL_REAGENTS_UPDATE") + ":");
        rUpdated.pack();
        
        reagentsUpdatedStatus = new Label(cGroup, SWT.RIGHT);
        reagentsUpdatedStatus.setLocation(80, 60);
        reagentsUpdatedStatus.setSize(65, 15);
        
        final Label dUpdated = new Label(cGroup, SWT.NONE);
        dUpdated.setLocation(10, 75);
        dUpdated.setText(localization.getString("LABEL_DUNGEONS_UPDATE") + ":");
        dUpdated.pack();
        
        dungeonsUpdatedStatus = new Label(cGroup, SWT.RIGHT);
        dungeonsUpdatedStatus.setLocation(80, 75);
        dungeonsUpdatedStatus.setSize(65, 15);
        
        final Label sUpdated = new Label(cGroup, SWT.NONE);
        sUpdated.setLocation(10, 90);
        sUpdated.setText(localization.getString("LABEL_CRAFTS_SPEC_UPDATE") + ":");
        sUpdated.pack();
        
        craftsSpecializationsUpdatedStatus = new Label(cGroup, SWT.RIGHT);
        craftsSpecializationsUpdatedStatus.setLocation(80, 90);
        craftsSpecializationsUpdatedStatus.setSize(65, 15);
        
        log.trace("Creating close button.");
        
        final Button closeButton = new Button(shell, SWT.PUSH);
        closeButton.setText(localization.getString("BUTTON_CLOSE"));
        closeButton.setSize(80, 25);
        closeButton.setLocation(125, 190);
        closeButton.addSelectionListener(new CloseSelectionListener());
    }
    
    /**
     * Populates the recap window with various controls.
     */
    private void fillComponents()
    {
        for (ResultCharacter rc : result.getImportedCharacters())
        {
            charactersList.add(rc.getName());
        }
        
        charactersList.select(0);
        ListSelectionListener.updateCharacterInfo(this);
    }
    
    /**
     * Returns the characters list.
     * @return A List widget.
     */
    public List getCharactersList()
    {
        return charactersList;
    }
    
    /**
     * Returns the character update label.
     * @return A {@link Label}
     */
    public Label getCharacterUpdateStatusLabel()
    {
        return characterUpdateStatus;
    }

    /**
     * Returns the professions deleted label.
     * @return A {@link Label}
     */
    public Label getProfessionsDeletedStatusLabel()
    {
        return professionsDeletedStatus;
    }

    /**
     * Returns the crafts updated label.
     * @return A {@link Label}
     */
    public Label getCraftsUpdatedStatusLabel()
    {
        return crafatsUpdatedStatus;
    }

    /**
     * Returns the reagents updated label.
     * @return A {@link Label}
     */
    public Label getReagentsUpdatedStatusLabel()
    {
        return reagentsUpdatedStatus;
    }

    /**
     * Returns the dungeons updated label.
     * @return A {@link Label}
     */
    public Label getDungeonsUpdatedStatusLabel()
    {
        return dungeonsUpdatedStatus;
    }
    
    /**
     * Returns the crafts specializations updated label.
     * @return A {@link Label}
     */
    public Label getCraftsSpecializationsUpdatedStatusLabel()
    {
        return craftsSpecializationsUpdatedStatus;
    }
    
    /**
     * Returns the upload results on which this window is based.
     * @return A {@link Result} object.
     */
    public Result getResult()
    {
        return result;
    }
}
